package cn.client.ui.view.main.mindfulness

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController


@Composable
fun MindfulnessHome(
    navController: NavController,
    viewModel: MindfulnessViewModel = hiltViewModel()
) {
    val selectedTabIndex = mutableStateOf(0)
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {

        /* 顶部栏 */
        MindfulnessHomeTopBar()

        /* 指示器 */
        MindfulnessTabContent(navController, viewModel, selectedTabIndex)
    }
}



// 指示器
@Composable
fun MindfulnessTabContent(
    navController: NavController,
    viewModel: MindfulnessViewModel,
    selectedTabIndex: MutableState<Int>
) {
    TabRow(
        selectedTabIndex = selectedTabIndex.value,
        modifier = Modifier.fillMaxWidth()
    ) {
        /* 推荐 */
        Tab(
            selected = selectedTabIndex.value == 0,
            onClick = { selectedTabIndex.value = 0 },
            text = { Text(text = "首页") }
        )
        /* 正念 */
        Tab(
            selected = selectedTabIndex.value == 1,
            onClick = { selectedTabIndex.value = 1 },
            text = { Text(text = "正念") }
        )
        /* 团体 */
        Tab(
            selected = selectedTabIndex.value == 2,
            onClick = { selectedTabIndex.value = 2 },
            text = { Text(text = "团体") }
        )
        /* 收藏 */
        Tab(
            selected = selectedTabIndex.value == 3,
            onClick = { selectedTabIndex.value = 3 },
            text = { Text(text = "收藏") }
        )
        /* 打卡 */
        Tab(
            selected = selectedTabIndex.value == 4,
            onClick = { selectedTabIndex.value = 4 },
            text = { Text(text = "打卡") }
        )
    }
    MindfulnessContent(navController, viewModel, index = selectedTabIndex.value)

}


// 内容路由
@Composable
fun MindfulnessContent(
    navController: NavController,
    viewModel: MindfulnessViewModel,
    index: Int
) {
    when (index) {
        0 -> {
            // 首页推荐
            MindfulnessHomeRecommend(navController, viewModel)
        }

        1 -> {
            // 正念资源
            MindfulnessMedia(navController, viewModel)
        }

        2 -> {
            // 正念团体
            MindfulnessGroup(navController, viewModel)
        }
        3 -> {
            // 收藏
            MindfulnessCollect(navController, viewModel)
        }

        4 -> {
            // 打卡
            MindfulnessLog(viewModel)
        }
    }
}

// 顶部栏
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MindfulnessHomeTopBar() {
    TopAppBar(
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.primaryContainer,
            titleContentColor = MaterialTheme.colorScheme.primary
        ),
        title = { Text("正念") },
        /* 左边按钮 */
        navigationIcon = {
            Spacer(modifier = Modifier.size(24.dp))
        },
        /* 右边按钮（可以是多个） */
        actions = {
            IconButton(onClick = {

            }
            ) {
                Icon(Icons.Filled.MoreVert, null)
            }
        }
    )

}